<?php

namespace app\command;

use app\logic\es\EsUser;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;


class EsUserCommand extends Command
{
    // 执行导入用户到es的操作
    protected function configure()
    {
        $this->setName('esUser')
            ->addArgument('type', Argument::OPTIONAL, "操作类型" )
            ->addOption('uid', '', Option::VALUE_OPTIONAL, "用户ID")
            ->setDescription('Es 用户导入');
    }

    protected function execute(Input $input, Output $output)
    {
        $type = trim($input->getArgument('type'));

        switch ($type) {
            case 'deleteIndex':
                EsUser::getInstance()->deleteIndex();
                break;
            case 'import':
                if ($input->hasOption('uid')) {
                    $uid = trim($input->getOption('uid'));
                    if (!empty($uid)) {
                        if ($uid == "all") {
                            EsUser::getInstance()->importAll();
                        } else {
                            $uid =  explode(',', trim($input->getOption('uid')));
                            foreach ($uid as $v) {
                                EsUser::getInstance()->import($v);
                            }
                        }
                    }
                } else {
                    $output->writeln("请输入uid");
                    return;
                }
                break;
            case 'initIndexSetting':
                $esUser = EsUser::getInstance();
                if ($esUser->indexExists()) {
                    EsUser::getInstance()->deleteIndex();
                }
                $res = EsUser::getInstance()->initIndexSetting();
                if ($res->asArray()) {
                    EsUser::getInstance()->importAll();
                    $output->writeln("初始化成功".json_encode($res->asArray()));
                } else {
                    $output->writeln("初始化失败");
                }
                break;
            default:
                $output->writeln("请输入正确的参数");
                return;
        }

        $output->writeln("执行完毕");
    }
}
